package com.qf.cls.application.tcc;

import com.qf.cls.application.entity.Cls;
import com.qf.cls.application.service.ClsService;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ClsTccImpl implements IClsTcc{

    @Autowired
    private ClsService clsService;

    @Override
    public int insert(Cls cls) {
        System.out.println("执行了Tcc的准备方法.....");
        clsService.save(cls);
        return 1;
    }

    @Override
    public boolean myCommit(BusinessActionContext businessActionContext) {
        System.out.println("执行了Tcc的Commit方法.....");
        return true;
    }

    @Override
    public boolean myRollback(BusinessActionContext businessActionContext) {
        System.out.println("执行了Tcc的Rollback方法.....");
        //从上下文中获取需要回滚的记录
        Cls cls = (Cls) businessActionContext.getActionContext("cls", Cls.class);
        System.out.println("从上下文中获取需要回滚的对象：" + cls);
        clsService.removeById(cls.getId());
        return true;
    }
}
